package com.desaysv.svview.refresh

import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.widget.LinearLayout
import com.desaysv.svview.R
import com.desaysv.svview.databinding.LayoutRefreshHeaderBinding

class DefaultRefreshHeader@JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyleAttr: Int = 0
) : BaseRefreshHeader(context,attributeSet,defStyleAttr){
    companion object{
        private const val TAG = "DefaultRefreshHeader"
    }
    private val binding= LayoutRefreshHeaderBinding.inflate(LayoutInflater.from(context))
    private var width=0
    private var height=0
    private var loadingAnimator: ValueAnimator?=null
    init {
        val layoutParams=LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)
        addView(binding.root,layoutParams)
    }

    override fun startRefresh() {
        if(loadingAnimator!=null&&loadingAnimator!!.isRunning){
            return
        }
        binding.ivArrow.setBackgroundResource(R.drawable.refresh_ing)
        loadingAnimator?.cancel()
        loadingAnimator= ValueAnimator.ofFloat(0f,1f).apply {
            duration=600
            repeatCount=ValueAnimator.INFINITE
            repeatMode=ValueAnimator.RESTART
            addUpdateListener {
                val value=it.animatedValue as Float
                binding.ivArrow.rotation=value*360f
            }
            start()
        }
        binding.tvTips.setText("刷新中...")
    }

    override fun stopRefresh(listener: StopRefreshListener) {
        loadingAnimator?.cancel()
        binding.ivArrow.rotation=0f
        binding.ivArrow.setBackgroundResource(R.drawable.refresh_arrow_down)
        binding.tvTips.setText("刷新完成")
        postDelayed({
            binding.tvTips.setText("下拉刷新")
            listener.stopComplete()
        },1000)

    }

    override fun headerTouch(realMove: Float, orientation: Int) {
        val ignoreMove=height/5*4
        val move=if(realMove< ignoreMove){
            0f
        }else{
            realMove-ignoreMove
        }
        val percent=if(move>=height&&orientation==LinearLayout.VERTICAL){
            1f
        }else if(move<height&&orientation==LinearLayout.VERTICAL){
            move /height
        }else if(move>=width&&orientation==LinearLayout.HORIZONTAL){
            1f
        }else if(move<width&&orientation==LinearLayout.HORIZONTAL){
            move /width
        }else{
            0f
        }
        binding.ivArrow.rotation=percent*180f
        binding.tvTips.text="松开即可刷新"
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //测量控件的真实高度
        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)
        if (width == 0 || height == 0) {
            return
        }
        if (width == this.width && height == this.height) {
            return
        }

        this.width = width
        this.height = height
        Log.i(TAG, "onMeasure: width=$width , height=$height")
    }
}